package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Component
@RequestMapping("/admin/dish")
@Slf4j
@Tag(name = "菜品管理接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishMapper dishMapper;

    @GetMapping("/page")
    @Operation(summary = "分页查询菜品列表", description = "分页查询菜品列表")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("用户分页查询参数: {}", dishPageQueryDTO);
        PageResult page = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(page);
    }

    @PostMapping
    @Operation(summary = "新增菜品", description = "新增菜品")
    public Result<Dish> save(@RequestBody DishDTO dishDTO) {
        log.info("用户保存菜品参数: {}", dishDTO);
        Dish dish = dishService.save(dishDTO);
        return Result.success(dish);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取菜品的详情")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("用户传递的菜品Id，{}", id);
        DishVO dish = dishService.getById(id);
        return Result.success(dish);
    }

    @PutMapping
    @Operation(summary = "更新菜品", description = "更新菜品")
    public Result<Dish> update(@RequestBody DishDTO dishDTO) {
        log.info("用户跟新菜品参数: {}", dishDTO);
        Dish dish = dishService.update(dishDTO);
        return Result.success(dish);
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "用户修改启售停售")
    public Result<String> changeStatus(@PathVariable Integer status, @RequestParam Long id) {
        log.info("用户传递的状态值: {}， 用户传递的id: {}", status, id);
        dishService.updateStatus(status, id);
        return Result.success("");
    }

    @DeleteMapping
    @Operation(summary = "批量删除/删除")
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("用户想要删除的id数组：{}", ids);
        dishService.deleteById(ids);
        return Result.success("");
    }


    @GetMapping("/list")
    @Operation(summary = "根据菜品分类id查询菜品")
    public Result<List<Dish>> getDishByCategoryId(Long categoryId) {
        log.info("用户想要查询的的categoryId：{}", categoryId);
        List<Dish> dishes = dishService.getDishByCategoryId(categoryId);
        return Result.success(dishes);
    }
}
